package com.hanbang.mrcm.aboutme;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;

import com.hanbang.mrcm.R;
import com.hanbang.mrcm.base.BaseActivity;
import com.hanbang.mrcm.utils.HttpInterfaces;
import com.hanbang.mrcm.utils.MyRequestCallBack;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class AboutAppActivity extends BaseActivity {

	private WebView webView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about_app);
		initView();
		setOnClickListener();
		getServiceData();
	}

	/*
	 * 设置点击事件
	 */
	private void setOnClickListener() {

	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			default:
				break;
			}
		}
	};

	/*
	 * 初始化view
	 */
	private void initView() {
		// 设置顶部的标题
		setBackArrows(findViewById(R.id.top_bar_left_image));
		setTitle(findViewById(R.id.top_bar_title), "关于我们");

		webView = (WebView) findViewById(R.id.webview);
		configWebview();

	}

	@SuppressLint("SetJavaScriptEnabled")
	private void configWebview() {
		// 设置文本编码
		webView.getSettings().setDefaultTextEncodingName("utf-8");
		// 可以缩放
		webView.getSettings().setSupportZoom(true);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		// 开启 Application Caches 功能
		webView.getSettings().setAppCacheEnabled(false);
		webView.getSettings().setLoadWithOverviewMode(true);
		// webView.getSettings().setDefaultZoom(ZoomDensity.MEDIUM);// 默认缩放模式
		webView.getSettings().setUseWideViewPort(false);
	}

	private String getFont() {
		StringBuilder builder = new StringBuilder();
		// <body class="MsoNormal"> </body>
		builder.append(".Likun { font-family: fzlt;}");
		builder.append("@font-face {font-family:fzlt;");
		builder.append("src:url(file:///android_asset/fzlt.ttf);}");
		return builder.toString();
	}

	/*
	 * 获取到服务器数据后开始跟新UI
	 */
	private void upDateUI(String content) {
		// 配置webview数据源
		StringBuilder data = new StringBuilder();
		data.append("<style type=\"text/css\">*{ line-height:30px;}*{ font-size:18px;}img{WIDTH:80% !important;HEIGHT:auto !important;}");
		data.append(getFont());
		data.append("</style><base href=\"");
		data.append(HttpInterfaces.ORG);
		data.append("\"/><body class=\"Likun\" style=\"padding-left:2%;padding-right:2%;\">");
		data.append(content);
		data.append("</body>");

		// 设置webview 加载数据
		webView.loadDataWithBaseURL(null, data.toString(), "text/html",
				"utf-8", null);
	}

	private void getServiceData() {
		httpUtils.send(HttpMethod.GET, HttpInterfaces.about_us,
				new MyRequestCallBack<String>(this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							int status = json.getInt("status");
							if (status == 1) {
								JSONObject result = json.getJSONArray("result")
										.getJSONObject(0);
								upDateUI(result.getString("content"));
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}
